﻿package cn.poco.pepsi.activity.search;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.LinearLayout;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import cn.poco.pepsi.R;
import cn.poco.pepsi.bean.Area;
import cn.poco.pepsi.service.impl.SearchImpl;
import cn.poco.pepsi.util.Cons;

/**
 * 搜索主页面更多地区
 * @author tanglx
 *
 */
public class SearchMainMoreAreaActivity extends Activity {

	private final static String TAG = "SearchMainMoreActivity";
	private final static int UPDAE_DATA = 100;
	private final static int UPDAE_SUB_DATA = 101;

	// 数据
	private ArrayList<Area> data;
	private List<Map<String, String>> groupData;
	private List<List<Map<String, String>>> childData;
	private SimpleExpandableListAdapter adapter;

	private ExpandableListView listview;
	private LinearLayout progressbar;

	private HandlerThread handlerThread;
	private Handler myHandler;

	private String citycode;
	private TextView showTitle;

	private void initView() {
		listview = (ExpandableListView) findViewById(R.id.expand_listview);
		listview.setCacheColorHint(0);
		progressbar = (LinearLayout) findViewById(R.id.progressbar);
		citycode = getIntent().getExtras().getString(Cons.CITY_CODE);
		showTitle = (TextView) findViewById(R.id.show_title);
		showTitle.setText("商区");
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.search_main_more_area);
		initView();

		// 启动线程
		handlerThread = new HandlerThread("thread03");
		handlerThread.start();
		myHandler = new Handler(handlerThread.getLooper());
		myHandler.post(new GetArea());

		listview.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
				String circle_code = childData.get(groupPosition).get(childPosition).get("ID");
				String circle_name = childData.get(groupPosition).get(childPosition).get("NAME");
				Intent intent = new Intent(SearchMainMoreAreaActivity.this, SearchResultActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString(Cons.CIRCLE_CODE, circle_code);
				bundle.putString(Cons.CITY_NAME, "");
				bundle.putString(Cons.AREA_CODE, "");
				bundle.putString(Cons.ACTIVITY_TOP_TITLE, circle_name);
				bundle.putString(Cons.ISPEPSI_RES, "no");
				intent.putExtras(bundle);
				startActivity(intent);
				return false;
			}
		});

		listview.setOnGroupExpandListener(new OnGroupExpandListener() {
			@Override
			public void onGroupExpand(int groupPosition) {
				myHandler.post(new GetSubArea(groupPosition, citycode, data.get(groupPosition).getKey()));

			}
		});
	}

	private final class GetArea implements Runnable {

		@Override
		public void run() {
			SearchImpl search = new SearchImpl(SearchMainMoreAreaActivity.this);
			try {
				data = search.getArea(citycode,SearchMainMoreAreaActivity.this);
				handler.sendEmptyMessage(UPDAE_DATA);
			} catch (Exception e) {
				handler.post(new Runnable() {
					@Override
					public void run() {
						progressbar.setVisibility(View.GONE);
					}
				});
				Log.e(TAG, e.toString());
				e.printStackTrace();
			}
		}
	}

	// 获取子数据
	private final class GetSubArea implements Runnable {
		private String citycode;
		private String lid;
		private int group_pos;

		public GetSubArea(int group_pos, String citycode, String lid) {
			this.group_pos = group_pos;
			this.citycode = citycode;
			this.lid = lid;
		}

		@Override
		public void run() {
			SearchImpl search = new SearchImpl(SearchMainMoreAreaActivity.this);
			try {
				ArrayList<Area> curChildData = search.getSubArea(citycode, lid);

				List<Map<String, String>> children = new ArrayList<Map<String, String>>();
				for (int j = 0; j < curChildData.size(); j++) {
					Map<String, String> childmap = new HashMap<String, String>();
					childmap.put("NAME", curChildData.get(j).getName());
					childmap.put("ID", curChildData.get(j).getKey());
					children.add(childmap);
				}
				childData.set(group_pos, children);
				handler.sendEmptyMessage(UPDAE_SUB_DATA);
			} catch (Exception e) {
				Log.e(TAG, e.toString());
				e.printStackTrace();
			}
		}
	}

	// UI刷新
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case UPDAE_DATA:
				groupData = new ArrayList<Map<String, String>>();
				childData = new ArrayList<List<Map<String, String>>>();
				for (int i = 0; i < data.size(); i++) {
					Map<String, String> groupMap = new HashMap<String, String>();
					groupMap.put("NAME", data.get(i).getName());
					groupData.add(groupMap);

					// 默认子数据
					List<Map<String, String>> children = new ArrayList<Map<String, String>>();

					Map<String, String> childmap = new HashMap<String, String>();
					childmap.put("NAME", "加载中");
					children.add(childmap);

					childData.add(children);
				}
				adapter = new SimpleExpandableListAdapter(SearchMainMoreAreaActivity.this, groupData,
						android.R.layout.simple_expandable_list_item_1, new String[] { "NAME" },
						new int[] { android.R.id.text1 },
						childData, android.R.layout.simple_expandable_list_item_1, new String[] { "NAME" },
						new int[] { android.R.id.text1 });

				listview.setAdapter(adapter);
				progressbar.setVisibility(View.GONE);
				listview.setVisibility(View.VISIBLE);
				break;
			case UPDAE_SUB_DATA:
				adapter.notifyDataSetChanged();
				break;

			}

		}

	};
}
